package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author feng js
 */
@Service
@Transactional
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper,ApLikesBehavior> implements ApLikesBehaviorService {

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * app端文章点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        if (dto.getArticleId() == null || dto.getOperation() > 1 || dto.getOperation() < 0 || dto.getType() > 1 || dto.getType() < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }


        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(),dto.getEquipmentId());
        if (behaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApLikesBehavior likesBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, behaviorEntry.getId()));
        if (likesBehavior == null && dto.getOperation().equals(ApLikesBehavior.Operation.CANCEL.getCode())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"没有点赞");
        }
        //发送分数修改
        if (likesBehavior != null && likesBehavior.getOperation() == 1) {
            UpdateArticleMess mess = new UpdateArticleMess();
            mess.setAdd(1);
            mess.setArticleId(likesBehavior.getArticleId());
            mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));

        }
        if (likesBehavior == null && dto.getOperation().equals(ApLikesBehavior.Operation.LIKE.getCode())) {
            //点赞
            likesBehavior = new ApLikesBehavior();
            likesBehavior.setArticleId(dto.getArticleId());
            likesBehavior.setEntryId(behaviorEntry.getId());
            likesBehavior.setCreatedTime(new Date());
            likesBehavior.setOperation(dto.getOperation());
            likesBehavior.setType((short) 0);
            save(likesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } else {
            //取消点赞
            likesBehavior.setOperation(dto.getOperation());
            updateById(likesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

    }
}
